// pages/shellForm/shellForm.js
import Router from '../../utils/router.js';
import goods from '../../services/goods.js';
import service from '../../services/shellForm';
import service_conch from '../../services/conch';
import util from '../../utils/util.js';
import base from '../../config/base.js';
var sharetimeFlag = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    link:'https://kpxd.zhengdianhaohuo.com/h5/#/buyer/win3_868',
    num: '0',
    receiveBlun: false,
    receiveData: {},
    /*判断用户是否在有效期内，并且没有领取过贝壳弹窗*/
    helpBlun: false,
    /*好友助力 */
    useData: {},
    listData: [],
    listBlue: false, //排行榜显示
    timeBlue: false,
    paihangbang: base.static + 'shellForm/xin333.png',
    map1: base.static + 'shellForm/1map.png',
    map2: base.static + 'shellForm/2map.png',
    map3: base.static + 'shellForm/3map.png',
    guize: base.static + 'shellForm/guize.png',
    arrow_img: base.static + 'aftersale/GroupCopy.png',
    sharepath: '',
    conchList: [], //2.12.0贝壳特惠列表
    pages: 0,
    loadBloon: true,
    endBloon: true,
    pageConch: 0,
    endConchBloon: true,
    loadConchBloon: true,
    friends_avatars: '',
    get_conch_num_str: '',
    getconch_flag:'',
    mask_box: false,
    transmit: {
      style: 'width: 325rpx; height: 100rpx;font-size: 34rpx;color: #66CC66;text-align: center;line-height: 100rpx;',
      value: '去抽奖'
    },
    share_time_flag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    let user_info = wx.getStorageSync('user_info')
    _this.setData({
      store_id: user_info.lastView.seid,
      user_id: user_info.id
    })
    _this.getData();
    _this.getShareImage();
    _this.shellBlue();
    _this.getConch(1)
    _this.getuserheadimg();
    

  },
  //获取数据
  getData() {
    let _this = this;
    let params = {}
    service.getRankingList(params, function(res) {
      if (res.code && res.code == 1) {
        _this.setData({
          useData: res.data.user_data || {},
          listData: res.data.list || [],
          getconch_flag: res.data.user_data.get_conch_num
        });
        _this.getestimate();//给预计贝壳数做分割
        if (res.data.list.length > 0 && res.data.list && res.data.list != null) {
          _this.setData({
            listBlue: true
          })
        } else {
          listBlue: false
        }
      } else {

      }
    });
    wx.stopPullDownRefresh()
  },
  //给预计贝壳数做分割
  getestimate(){
    let get_conch_num = this.data.useData.get_conch_num
    let get_conch_num_arr = get_conch_num.toString().split("");
    let arr_sum_str = '';
    if (get_conch_num_arr.length > 3) {
      let get_conch_num_new = get_conch_num_arr.reverse();
      let arr1 = [];
      let arr2 = [];
      for (let i = 0; i < get_conch_num_new.length; i++) {
        if (i < 3) {
          arr1[arr1.length] = get_conch_num_new[i];
        } else {
          arr2[arr2.length] = get_conch_num_new[i];
        }
      }
      arr1.push(",")
      let arr_sum = arr1.concat(arr2);
      arr_sum_str = arr_sum.reverse().join("");
      this.setData({
        get_conch_num_str: arr_sum_str
      })
    }else {
      this.setData({
        get_conch_num_str: get_conch_num
      })
    }
  },
  //2.12.0贝壳特惠列表
  getConch: function (page) {
    let that = this;
    let params = {
      limit: 20,
      offset: page
    }
    service_conch.getConchGoods(params, function (res) {
      if (res.code && res.code == 200) {
        let stolist = that.data.conchList.concat(res.data.list)
        if (stolist != ""){
          stolist.forEach((item, index) => {
            item.goods_pic = [{ url: '' }]
            item.goods_pic[0].url = item.src
          })
        }
        that.setData({
          conchList: stolist,
          endBloon: true,
          pages: page + 1,
        })
        if (stolist.length >= res.data.count){
          that.setData({
            loadBloon: false
          })
        }
      }
    });
    wx.stopPullDownRefresh()
  },
  //获取分享图片
  getShareImage() {
    let _this = this;
    let params = {}
    service.getShareImage(params, function(res) {
      if (res.code && res.code == 1) {
        _this.setData({
          sharepath: res.data.share_img || ""
        });

      }
    });
  },
  //获取昨天贝壳数判断
  shellBlue() {
    let _this = this;
    let params = {}
    params.type = 'check_day_conch'
    service.shellBlue(params, function(res) {
      if (res.code && res.code == 1) {
        _this.setData({
          receiveBlun: true,
          receiveData: res.data,
        });

      } else {
        _this.setData({
          receiveBlun: false
        })
      }
    });
  },

  //查看页面规则
  goRule(e) {
    //Router.go('rule'); 小程序规则页面
    var link = encodeURIComponent(e.currentTarget.dataset.link);
    if (link) {
      Router.go('web', `url=${link}`, 'navigateTo');
    }
  },
  //立即邀请
  inviteJs() {
    
  },
  //立即领取贝壳
  receiveJs() {
    let _this = this;
    let params = {}
    params.type = 'receive_day_conch'
    service.shellBlue(params, function(res) {
      if (res.code && res.code == 1) {
        _this.setData({
          receiveBlun: false,
        });
        wx.showToast({
          title: '领取成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        _this.setData({
          receiveBlun: false,
        });
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  //立即邀请好友
  recordClickBtn() {
    this.setData({
      helpBlun: false,
    });
  },
  //监听页面高度
  scrollTimer: null,
  onPageScroll: function (e) {
    let that = this;
    if (that.scrollTimer) {
      clearTimeout(that.scrollTimer)
    };
    that.scrollTimer = setTimeout(() => {
      that.setData({
        scrolltop: e.scrollTop
      })
    }, 500);
  },
  //去召唤好友页面
  gomyfirend() {
    Router.go('myfirend')
  },
  //召唤好友头像
  getuserheadimg() {
    let that = this;
    service_conch.showrecallfriendpic({}, function (res) {
      if (res && res.code == 1) {
        that.setData({
          friends_avatars: res.data,
        })
      }
    })
  },

  // 去抽奖
  goluck() {
    this.setData({
      mask_box: false
    })
    Router.go('luck')
  },
  // 一会再抽
  cancleluck() {
    this.setData({
      mask_box: false
    })
  },
  //抽奖提示
  choujiangHandle(){
    let that = this;
    service.invitedsign({}, function (res) {
      if (res && res.code == 1) {
        if (res.data.sign == 0) {
          that.setData({
            mask_box: false
          })
        } else if (res.data.sign == 1) {
          that.setData({
            mask_box: true
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (sharetimeFlag) {
      clearTimeout(sharetimeFlag)
    }
    if (this.data.share_time_flag){
      //this.choujiangHandle();//抽奖提示      
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let _this = this;
    _this.getData();
    _this.setData({
      conchList: [], //2.12.0贝壳特惠列表
      pages: 1,
      loadBloon: true,
      endBloon: true,
    })
    _this.getConch(1)
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this;
    if (that.data.loadBloon && that.data.endBloon) {
      that.setData({
        endBloon: false
      })
      that.getConch(that.data.pages);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let that = this;
    if (that.data.share_time_flag){
      that.setData({
        share_time_flag:false
      })
    }
    sharetimeFlag = setTimeout(()=>{
      that.setData({
        share_time_flag: true
      })
    },3000)
    let title_arr = ['每天点一点，省钱不只一点点！', '点击签到，马上领钱！', '赚大了！连续签到7天就能得3元，有福同享，点就有', '帮我点一下，就能一起领钱', '哈哈哈！签到就能领钱，你也快来吧', '天生爱分享！签到领钱你听说过吗？', '不好意思，这钱是你掉的吗？', '天上掉馅饼啦！每天签到，就能领钱', '有些钱，该领就得领', '有钱不领，你是认真的吗？']
    let index = Math.floor(Math.random() * (title_arr.length));
    let paths = '';
    paths = 'pages/store/index?to=' + encodeURIComponent('/pages/conch/conch?share_id=' + that.data.user_id) + '&store_id=' + that.data.store_id + '&sharerecord=shellFormcard' + '&user_id=' + that.data.user_id;
    return {
      title: title_arr[index],
      path: paths,
      imageUrl: 'https:' + that.data.sharepath
    }
   
  }
})